﻿var ROOT_PATH = $("#root_path").val();   //get base_url

function focusCheck(dom, str) {  //display pre-set hints when foucus on current textbox
  $(dom).attr("class", "on").next("p").attr("class", "prompt").text(str); 
}

function success(dom) {  //display a check image when input text is valid
  $(dom).attr("class", "").next("p").attr("class", "").html('<img src="/mysite/images/gou.gif" />'); 
}

var emailchecked = false;  //email validation check
function emailBlur(dom) {
    var email = $(dom).val();
    if ($.trim(email).length <= 0) { 
    	$(dom).attr("class", "").val("").next("p").attr("class", ""); 
    	emailchecked = false; 
    }
    else {
        var reg = new RegExp("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
        if (reg.test($(dom).val())) {
        	$.ajax({
                type: "get",
                url: ROOT_PATH + "register/emailexists?email=" +email.toLowerCase() ,
                dataType: "text",
                contentType: "application/json;utf-8",
                //data: "{email:'" + email.toLowerCase() + "'}",
                timeout: 10000,
                error: function(a, b, c) { 
                	emailchecked = true; 
                	$(dom).attr("class", "").next("p").attr("class", ""); 
                },
                success: function(msg) {                    
                	if (msg=='true') {
                        $(dom).attr("class", "wrong").next("p").attr("class", "red").text("Email地址已存在");
                        emailchecked = false;
                    }
                    else {
                        success(dom);
                        emailchecked = true;
                    }
                }
            });
        }
        else {
            $(dom).attr("class", "wrong").next("p").attr("class", "red").text("Email格式不正确");
            emailchecked = false;
        }
    }
}

var nicknamechecked = false;
function nicknameblur(dom) {
    var nickname = $.trim($(dom).val());
    if (nickname.length <= 0) { 
    	$(dom).attr("class", "").val("").next("p").attr("class", "").text("用户名也可以作为您登录Qiqu Taobao的帐号，大家以后就这样称呼您"); 
    	nicknamechecked = false; 
    }
    else {
    	var regtest = new RegExp("^([\\u4e00-\\u9fa5]|\\w)*$");
        var regunderline = new RegExp("_");
        if (regtest.test(nickname) && regunderline.exec(nickname) == null) {
        	
        	var regEng = new RegExp("\\w", "g");
            var regCn = new RegExp("[\\u4e00-\\u9fa5]", "g");
            var nicknamelength = 0;
            var res;
            while ((res = regEng.exec(nickname)) != null)
                nicknamelength++;
            while ((res = regCn.exec(nickname)) != null)
                nicknamelength += 2;

            if (nicknamelength <= 16 && nicknamelength >= 4) {
            	$.ajax({
                    type: "get",
                    url: ROOT_PATH +  "register/usernameexists?username=" + nickname.toLowerCase(),
                    dataType: "text",
                    contentType: "application/json;utf-8",
                    //data: "{username:'" + nickname.toLowerCase() + "'}",
                    timeout: 10000,
                    error: function() { 
                    	nicknamechecked = true; 
                    	$(dom).attr("class", "").next("p").attr("class", ""); 
                    },
                    success: function(msg) {
                    	if (msg=='true') {
                            $(dom).attr("class", "wrong").next("p").attr("class", "red").text("用户名已存在");
                            nicknamechecked = false;
                        }
                        else {
                            success(dom);
                            nicknamechecked = true;
                        }
                    }
                });
            }
            else {
                $(dom).attr("class", "wrong").next("p").attr("class", "red").text("用户名长度必须是2-8个汉字或4-16个字符");
                nicknamechecked = false;
            }
        }
        else {
        	$(dom).attr("class", "wrong").next("p").attr("class", "red").text("用户名必须是大小写英文字母、中文、数字组成，长度2-8个汉字或4-16个字符");
            nicknamechecked = false;
        }
    }
}

var passwordchecked = false;
function checkPassword(dom) {
	$("#passPower").hide();
    $(dom).next("p").show();
    var pass = $.trim($(dom).val());
    if (pass.length <= 0) { 
    	$(dom).attr("class", "").val("").next("p").attr("class", "").text("请输入您的帐号密码"); 
    	passwordchecked = false; 
    }
    else {
        var regtest = new RegExp("^(\\w)*$");
        if (regtest.test(pass)) {
            var regEng = new RegExp("\\w", "g");
            var passLength = 0;
            var res;
            while ((res = regEng.exec(pass)) != null)
                passLength++;

            if (passLength <= 20 && passLength >= 6) {
                success(dom);
                if ($("#regRePassword").val().length > 0) {
                    reCheckPassword("#regRePassword");
                }
                passwordchecked = true;
            }
            else {
                $(dom).attr("class", "wrong").next("p").attr("class", "red").text("密码长度必须是6-20个字符");
                passwordchecked = false;
            }
        }
        else {
            $(dom).attr("class", "wrong").next("p").attr("class", "red").text("密码必须是大小写英文字母、数字组成、长度为6-20个字符");
            passwordchecked = false;
        }
    }
}

function passPower(dom) {
    var pass = $(dom).val().toLowerCase();
    var powerRank = 0;
    if (pass.length > 0) {
        $(dom).next("p").hide();
        if (pass.length > 8) powerRank++;
        if (new RegExp("[0-9][a-z]|[a-z][0-9]").test(pass)) powerRank++;
        if (new RegExp("[0-9]+[a-z]+[0-9]+[a-z]|[a-z]+[0-9]+[a-z]+[0-9]").test(pass)) powerRank++;
        switch (powerRank) {
            case 0: $("#passPower").attr("class", "rank").text("安全等级：低"); break;
            case 1: $("#passPower").attr("class", "rank").text("安全等级：低"); break;
            case 2: $("#passPower").attr("class", "rank mid").text("安全等级：中"); break;
            case 3: $("#passPower").attr("class", "rank High").text("安全等级：高"); break;
            default: $("#passPower").attr("class", "rank").text("安全等级：低"); break;
        }
        $("#passPower").css("display", "inline");
    }
    else {
        $("#passPower").hide();
        $(dom).next("p").show();
    }
}

var repasschecked = false;
function reCheckPassword(dom) {
    var rePass = $.trim($(dom).val());
    if (rePass.length <= 0) {
        $(dom).attr("class", "").next("p").attr("class", "").text("请再输入一次您的密码");
        repasschecked = false;
    } 
    else {
        if ($.trim($("#regPassword").val()) == rePass) {
            success(dom);
            repasschecked = true;
        }
        else {
            $(dom).attr("class", "wrong").next("p").attr("class", "red").text("两次密码输入不一致，请重新输入");
            repasschecked = false;
        }
    }
}


var telphonechecked = false;
function telphoneblur(dom) {
    var telphone = $.trim($(dom).val());
    if (telphone.length <= 0) {  
    	$(dom).attr("class", "").val("").next("p").attr("class", "").text("如果有任何问题, 站长可以通过这个电话联系你"); 
        telphonechecked = false;
    }
    else {
            if (telphone.length >= 10 && telphone.length<=20) {
            	    success(dom);
                    telphonechecked = true;
            }
            else {
                $(dom).attr("class", "wrong").next("p").attr("class", "red").text("电话号码长度不应该小于10位或大于20位");
                telphonechecked = false;
            }
    }
}

var addresschecked = false;
function addressblur(dom) {
    var address = $.trim($(dom).val());
    if (address.length <= 0) {  
    	$(dom).attr("class", "").val("").next("p").attr("class", "").text("站长可以通过居住地址让居住在附近的朋友代你提货"); 
    	addresschecked = false; 
    }
    else {
    	if (address.length>255){    //max length of address is 255
           $(dom).attr("class","wrong").next("p").attr("class","red").text("地址的长度不可以超过255个英文字符或125个中文字符");
    		addresschecked = false; 
    	}
    	else{
           success(dom);
           addresschecked = true;
    	}
    }
}

function validatecode_success(dom) {  //display a check image when validation code is valid
  	$(dom).attr('class','verification').nextAll('p').show(); 
  	$(dom).attr("class","verification").nextAll("p").attr("class", "").html('<img src="/mysite/images/gou.gif" />'); 
}


var checkcodechecked = false;
function checkCode(dom) {
	var checkCode = $.trim($(dom).val()).toLowerCase();
    if (checkCode == "") {
        $(dom).attr("class", "verification").nextAll("p").hide(); 
        checkcodechecked = false;
    }
    else {
    	$.ajax({
            type: "get",
            url: ROOT_PATH + "register/iscodevalid?code=" + checkCode,
            dataType: "text",
            timeout: 10000,
            error: function(a, b, c) { 
            	checkcodechecked = true; 
            },
            success: function(msg) {
            	if (msg == 'false') {
                    checkcodechecked = false;
                    $(dom).attr('class', 'verification wrong').nextAll('p').attr("class", "red").text(" 验证码输入错误，请重新输入"); 
                    $(dom).attr("class", "verification wrong").nextAll("p").show();
                }
                else {
                    checkcodechecked = true;
                    validatecode_success(dom);
                }
            }
        });
    }
}

// when click code image to change code
function changeCode(dom, url){
	checkcodechecked = false;   //reset flag
	$(dom).attr('src', url + '?code=' + Math.random());            //upudate code image, if the link doesn't change , ie will not reload new pic
	$('#regCheckCodeInput').val(''); //delete old code in text box
    
    $(dom).attr('class', 'verification').nextAll('p').attr("class", "red").text(" 验证码输入错误，请重新输入"); 
	$(dom).attr("class", "verification").nextAll("p").hide(); 
}

function regSubmit() {
    if (!checkcodechecked) {
        var checkCode = $.trim($("#regCheckCodeInput").val()).toLowerCase();
        if (checkCode == "") {
            $("#regCheckCodeInput").attr("class", "verification").nextAll("p").show(); 
            checkcodechecked = false;
        }
        else {
            $.ajax({
                type: "get",
                url: ROOT_PATH + "register/iscodevalid?code=" + checkCode,
                dataType: "text",
                timeout: 5000,
                error: function(a, b, c) { 
                	checkcodechecked = true; 
                	regSucSubmit(); },
                success: function(msg) {
                    if (msg == 'false') {
                        checkcodechecked = false;
                        $("#regCheckCodeInput").attr("class", "verification wrong").nextAll("p").show();
                    }
                    else {
                        checkcodechecked = true;
                        $("#regCheckCodeInput").attr("class", "verification").nextAll("p").hide();
                        regSucSubmit();
                    }
                }
            });
        }
    }
    else { regSucSubmit(); }
}


function regSucSubmit() {   //check other 
    if ($.trim($("#regEmail").val()).length > 0) {
        if ($.trim($("#regNickName").val()).length > 0) {
            emailBlur($("#regEmail")); 
            nicknameblur($("#regNickName")); 
            checkPassword($("#regPassword")); 
            reCheckPassword($("#regRePassword"));
            telphoneblur($("#regTelphone"));
            addressblur(($("#regAddress")));
            if (emailchecked && 
            	nicknamechecked && 
            	passwordchecked && 
            	repasschecked && 
            	telphonechecked &&
            	addresschecked &&
            	document.getElementById("regAgree").checked) {
                $("form")[0].submit(); 
            } else {
                if (!document.getElementById("regAgree").checked){
                    alert("请仔细阅读注册协议并勾选同意复选框");
                } 
                else{
                    alert("请确认所有的项目都已经正确填写.");                	
                }
            }
        } else { $("#regNickName").attr("class", "wrong").next("p").attr("class", "red").text("请输入用户名"); }
    }
    else { 
    	$("#regEmail").attr("class", "wrong").next("p").attr("class", "red").text("请输入Email地址");
    }
}


$(document).ready(function() {
    $("#regCheckCodeInput").keydown(function(e) {
        if (e.keyCode == 13) regSubmit();
    })
    
});